package com.feige.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.dao.AskForLeaveMapper;
import com.feige.pojo.AskForLeave;
import com.feige.pojo.vo.AskForLeaveDetailVo;
import com.feige.pojo.vo.AskForLeaveVo;
import com.feige.service.AskForLeaveService;
import com.feige.result.AjaxResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 请假表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Service
public class AskForLeaveServiceImpl extends ServiceImpl<AskForLeaveMapper, AskForLeave> implements AskForLeaveService {


    @Resource
    private AskForLeaveMapper askForLeaveMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public List<AskForLeaveVo> getAskForLeaveById(long pageNum, long pageSize, String userId) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("pageNum",(pageNum - 1) * pageSize);
        map.put("pageSize",pageSize);
        map.put("userId",userId);
        return askForLeaveMapper.getAskForLeaveById(map);
    }

    @Override
    public int getAskForLeaveCountById(String userId) {
        return askForLeaveMapper.getAskForLeaveCountById(userId);
    }

    @Override
    public AskForLeaveDetailVo getAskForLeaveDetailById(String id,String audit) {
        return askForLeaveMapper.getAskForLeaveDetailById(id,audit);
    }

    @Override
    public List<AskForLeaveVo> getAskForLeaveByClazzId(Integer pageNum, long pageSize, String clazzId) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("pageNum",(pageNum - 1) * pageSize);
        map.put("pageSize",pageSize);
        map.put("clazzId",clazzId);
        return askForLeaveMapper.getAskForLeaveByClazzId(map);
    }

    @Override
    public long getAskForLeaveCountByClazzId(String clazzId) {
        return askForLeaveMapper.getAskForLeaveCountByClazzId(clazzId);
    }

    @Override
    public AjaxResult updateAskForLeave(AskForLeave askForLeave) {
        boolean update = this.updateById(askForLeave);
        if (update){
            Integer audit = askForLeave.getAudit();
            if (audit != null && audit.equals(1)){
                //发送邮箱
                HashMap<String, String> map = new HashMap<>(5);
                map.put("email",askForLeave.getEmail());
                map.put("text","编号为" + askForLeave.getId() + "的请假条已审核通过，请前往应用内查看" );
                rabbitTemplate.convertAndSend("EmailDirectExchange","EmailDirectRouting", map);
            }else if (audit != null && audit.equals(3)){
                //发送邮箱
                HashMap<String, String> map = new HashMap<>(5);
                map.put("email",askForLeave.getEmail());
                map.put("text","编号为" + askForLeave.getId() + "的请假条未审核通过，请前往应用内查看" );
                rabbitTemplate.convertAndSend("EmailDirectExchange","EmailDirectRouting", map);
            }
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}
